package org.tankwar.controller;

import org.tankwar.model.Bullets;
import org.tankwar.model.EnemyTank;
import org.tankwar.model.Tank;
import org.tankwar.view.GamePanel;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GameMenuBarController implements ActionListener {
    private final GamePanel gamePanel; // 游戏面板，用于模型的显示

    public GameMenuBarController(GamePanel gamePanel) {
        this.gamePanel = gamePanel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("NewGame")) {
            gamePanel.printable = false;
            Object[] options = { "确定", "取消" };
            int response = JOptionPane.showOptionDialog(gamePanel, "您确认要开始新游戏！", "", JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
            if (response == 0) {
                gamePanel.getMainFrame().setGamePanel(new GamePanel(gamePanel.getMainFrame()));
            } else {
                gamePanel.printable = true;
                new Thread(gamePanel.getPaintThread()).start(); // 线程启动
            }
        } else if (e.getActionCommand().endsWith("Stop")) {// 暂停
            gamePanel.printable = false;
        } else if (e.getActionCommand().equals("Continue")) {// 继续
            if (!gamePanel.printable) {
                gamePanel.printable = true;
                new Thread(gamePanel.getPaintThread()).start(); // 线程启动
            }
        } else if (e.getActionCommand().equals("Exit")) {// 退出
            gamePanel.printable = false;
            Object[] options = { "确定", "取消" };
            int response = JOptionPane.showOptionDialog(gamePanel, "您确认要退出吗", "", JOptionPane.YES_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
            if (response == 0) {
                System.out.println("退出");
                System.exit(0);
            } else {
                gamePanel.printable = true;
                new Thread(gamePanel.getPaintThread()).start(); // 线程启动
            }
        } else if (e.getActionCommand().equals("help")) {
            gamePanel.printable = false;
            JOptionPane.showMessageDialog(null,
                    "用→ ← ↑ ↓控制方向，F键盘发射，R重新开始！", "提示！",
                    JOptionPane.INFORMATION_MESSAGE);
            gamePanel.printable = true;
            new Thread(gamePanel.getPaintThread()).start(); // 线程启动
        } else if (e.getActionCommand().equals("level1")) {
            setGameLevel(12, 6, 6, 10, 10);
        } else if (e.getActionCommand().equals("level2")) {
            setGameLevel(12, 10, 10, 12, 12);
        } else if (e.getActionCommand().equals("level3")) {
            setGameLevel(20, 14, 14, 16, 16);
        } else if (e.getActionCommand().equals("level4")) {
            setGameLevel(20, 16, 16, 18, 18);
        }
    }

    /**
     * 设置游戏难度级别的方法
     * @param count 坦克数量
     * @param tankSpeedX 坦克横向移动速度
     * @param tankSpeedY 坦克纵向移动速度
     * @param bulletSpeedX 子弹横向移动速度
     * @param bulletSpeedY 子弹纵向移动速度
     */
    private void setGameLevel(int count, int tankSpeedX, int tankSpeedY,
                              int bulletSpeedX, int bulletSpeedY) {
        EnemyTank.count = count;
        Tank.speedX = tankSpeedX;
        Tank.speedY = tankSpeedY;
        Bullets.speedX = bulletSpeedX;
        Bullets.speedY = bulletSpeedY;
        gamePanel.printable = false;
        gamePanel.getMainFrame().setGamePanel(new GamePanel(gamePanel.getMainFrame()));
    }
}
